package land_register;

import java.io.IOException;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Clock;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;

import algorithm.Login_credit;
import algorithm.Point_record;
import DB.DB_connection_pool;


public class Login_index_filter implements Filter {


	public void destroy() {
		// TODO Auto-generated method stub
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

          String user_account = request.getParameter("user_account");
          
          if(user_account!=null){    //所有登陆都走这个过滤器，把用户信息放进session里面
          String nickname = "";
          String user_photo_path = "";
		
          String user_select = "select * from user_data where user_account='"+user_account+"'";
          
          try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement ps= conn.prepareStatement(user_select);
			ResultSet rs =  ps.executeQuery();
			while(rs.next()){
				nickname = rs.getString("user_nickname");
				user_photo_path = rs.getString("user_photo_path");
			}
			
			
			String select_sign = "select * from user_sign where user_account='"+user_account+"'";
	  		PreparedStatement ps_sign= conn.prepareStatement(select_sign);
			ResultSet rs_sign =  ps_sign.executeQuery();
			int sign_days;    //连续登陆天数
			Date last_day;    //上次登录时间
			
			Clock clock = Clock.systemUTC();
			java.sql.Date date = new java.sql.Date(clock.millis());     //当前时间
			if(rs_sign.next()){
				sign_days = rs_sign.getInt("sign_days");
				last_day = rs_sign.getDate("last_day");
			}else{     //第一次登陆
				String insert_sign = "insert into user_sign(user_account,sign_days,last_day) values(?,?,?)";
				PreparedStatement ps_insert = conn.prepareStatement(insert_sign);
				ps_insert.setString(1, user_account);
				ps_insert.setInt(2, 0);
				java.sql.Date date1 = new java.sql.Date(clock.millis()-(1000*60*60*24));     //昨天的时间
				ps_insert.setDate(3, date1);
				ps_insert.executeUpdate();
				
				sign_days = 0;
				last_day = date1;
			}
			Login_credit login_cre = new Login_credit();
			Map<String, Integer> map =login_cre.Get_logindetails(sign_days,last_day);   //返回map对象，map<连续登录天数，获得积分值>
			//System.out.println(map.get("logindays")+"     "+map.get("credit"));
			
			last_day = date;   //设置上次登录时间为本次时间
			
			//改变连续登录时间
			String update_sign = "update user_sign set last_day = '"+last_day+"' , sign_days = '"+map.get("logindays")+"' where user_account='"+user_account+"'";
			PreparedStatement ps_update = conn.prepareStatement(update_sign);
			ps_update.executeUpdate();
			ps_update.close();
			
			//改变积分,添加纪录
			//System.out.print(map.get("flag"));
			if(map.get("flag")==1){            //积分改变
				Point_record pr = new Point_record();
				pr.change_point(user_account,(double)(map.get("credit")));
				pr.point_record(user_account, Point_record.login_event, (double)(map.get("credit")), true);
			}
			
			conn.close();
			ps.close();
			rs.close();
			ps_sign.close();
			rs_sign.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
          
        HttpSession session = ((HttpServletRequest)request).getSession();
        session.setAttribute("user_account",user_account);     //将来自注册及登陆界面的账号，昵称存进session中
  		session.setAttribute("nickname", nickname);
  		if(user_photo_path!=null){
  			session.setAttribute("user_photo_path", user_photo_path);
  		}else{
  			session.setAttribute("user_photo_path", "");
  		}
  		
   
  		//System.out.println(user_account);
  		//System.out.println(nickname);
  		//System.out.println(user_photo_path);
          }
          
          
          
		chain.doFilter(request, response);
	}
	
	

	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
